package com.example.springaitest.controller;

import com.example.springaitest.repository.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ServiceController {

    private final ChatClient serviceChatClient;

    private final ChatHistoryRepository chatHistoryRepository;
    //流式调用
    @GetMapping(value = "/service",produces = "text/html;charset=utf-8")
    public Flux<String> serviceStream(@RequestParam("prompt") String message,@RequestParam("chatId") String chatId){
        System.out.println(message);
        chatHistoryRepository.save("service",chatId);
        return serviceChatClient.prompt()
                .user(message)
                .advisors(a->a.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,chatId))
                .stream()
                .content();
    }}
